package com.primaryedu.ui.app.growstep;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.primaryedu.R;
import com.primaryedu.data.Contants;

import java.util.ArrayList;

/**
 * Created by Thomas on 2018/2/9.
 */

public class ClassGridAdapter extends BaseAdapter {
    private final Context context;
    private final ArrayList<String> listUrls = new ArrayList<>();
    private final LayoutInflater inflater;

    public ClassGridAdapter(Context context, String pahts) {
        String[] strs = pahts.split(",");
        for (int i = 0, len = strs.length; i < len; i++) {
            System.out.println(strs[i].toString());
            listUrls.add(Contants.course_api + strs[i].toString());
        }

        Log.i("yy", "ClassGridAdapter: listUrls=======" + listUrls);
        Log.i("yy", "ClassGridAdapter: listUrls.size()=======" + listUrls.size());
        this.context = context;
        if (listUrls.size() == 7) {
            listUrls.remove(listUrls.size() - 1);
        }
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return listUrls.size();
    }

    @Override
    public String getItem(int position) {
        return listUrls.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.include_item_published_grida, parent, false);
            holder.image = (ImageView) convertView.findViewById(R.id.item_grida_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final String path = listUrls.get(position);
        Log.i("yy", "path===============: " + path);
        if (path.equals("paizhao")) {
            holder.image.setImageResource(R.drawable.icon_addpic_unfocused);
        } else {
            Glide.with(context)
                    .load(path)
                    .placeholder(R.drawable.default_error)
                    .error(R.drawable.default_error)
                    .centerCrop()
                    .crossFade()
                    .into(holder.image);
        }
        return convertView;
    }

    class ViewHolder {
        ImageView image;
    }
}
